//
//  XJHBaseNavViewController.swift
//  PublicPod
//
//  Created by 许俊华 on 2021/9/8.
//

import UIKit

open class XJHBaseNavViewController: UINavigationController {

    // 记录push转态
    var pushing:Bool = false
    
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        // Do any additional setup after loading the view.
    }
    
    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    public override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        //let attributeDic:NSDictionary = NSDictionary.init(objects: [UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium), UIColor.colorWithHexColor(hexadecimal: "#000000")], forKeys: [NSAttributedString.Key.font as NSCopying, NSAttributedString.Key.foregroundColor as NSCopying])
        //self.navigationBar.titleTextAttributes = (attributeDic as! [NSAttributedString.Key : Any])
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //每一次push都会执行这个方法，push之前设置viewController的hidesBottomBarWhenPushed
    open override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        viewController.hidesBottomBarWhenPushed = true
        if self.pushing == true {
            return
        } else {
            self.pushing = true
        }
        super.pushViewController(viewController, animated: true)
        viewController.hidesBottomBarWhenPushed = false
        
    }
    
    open override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
        //以下函数是对返回上一级界面之前的设置操作
        //每一次对viewController进行push的时候，会把viewController放入一个栈中
        //每一次对viewController进行pop的时候，会把viewController从栈中移除
        if self.children.count == 2
        {
            //如果viewController栈中存在的ViewController的个数为两个，再返回上一级界面就是根界面了
            //那么要对tabbar进行显示
            let controller:UIViewController = self.children[0]
            controller.hidesBottomBarWhenPushed = false
            
        }
        else
        {
            //如果viewController栈中存在的ViewController的个数超过两个，对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true
            //把tabbar进行隐藏
            viewController.hidesBottomBarWhenPushed = true
        }
        
        
        return super.popToViewController(viewController, animated: animated)
    }
    
    open override func popViewController(animated: Bool) -> UIViewController? {
        
        //以下函数是对返回上一级界面之前的设置操作
        //每一次对viewController进行push的时候，会把viewController放入一个栈中
        //每一次对viewController进行pop的时候，会把viewController从栈中移除
        if self.children.count == 2
        {
            //如果viewController栈中存在的ViewController的个数为两个，再返回上一级界面就是根界面了
            //那么要对tabbar进行显示
            let controller:UIViewController = self.children[0]
            controller.hidesBottomBarWhenPushed = false
            
        }
        else
        {
            //如果viewController栈中存在的ViewController的个数超过两个，对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true
            //把tabbar进行隐藏
            let count = self.children.count-2
            if count >= 0 {
                let controller = self.children[count]
                controller.hidesBottomBarWhenPushed = true
            }
            
        }
        
        
        return super.popViewController(animated: true)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK:UINavigationControllerDelegate
extension XJHBaseNavViewController : UINavigationControllerDelegate {
    
    open func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        self.pushing = false
    }

}
